package com.bovink.customalbum.album;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;

import com.bovink.customalbum.R;
import com.bovink.customalbum.ScreenUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Retina975
 * @since 2017/07/03
 */

public class FolderImageAdapter extends RecyclerView.Adapter<FolderImageAdapter.FolderImageHolder> {

    private Context context;

    private List<String> imgs = new ArrayList<>();
    private List<String> checkedImgs = new ArrayList<>();
    private String path;

    public FolderImageAdapter(Context context) {
        this.context = context;
    }

    @Override
    public FolderImageHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new FolderImageHolder(LayoutInflater.from(context).inflate(R.layout.item_recycler_folder_img, parent, false));
    }

    @Override
    public void onBindViewHolder(final FolderImageHolder holder, int position) {
        String imgPath = path + imgs.get(position);
        holder.imageCb.setChecked(checkedImgs.contains(imgPath));

        Glide.with(context)
                .load(imgPath)
                .placeholder(new ColorDrawable(0x80FFFFFF))
                .into(holder.imageIv);

        // 点击整个视图切换图片选中状态
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 当点击被选中的图片时,取消选中
                if (holder.imageCb.isChecked()) {
                    holder.imageCb.setChecked(false);
                    return;
                }

                // 当点击未选中的图片时分两种情况
                if (AlbumActivity.selectedImgNum < 9) {
                    holder.imageCb.setChecked(true);
                } else {
                    Toast.makeText(context, "你最多只能选择9张图片", Toast.LENGTH_SHORT).show();
                }
            }
        });

        // 图片选中状态变化事件
        holder.imageCb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // 当取消选择时
                if (!isChecked) {
                    AlbumActivity.relay.accept(new EventType.SelectImageEvent(
                            path + imgs.get(holder.getAdapterPosition()),
                            false));
                    return;
                }

                // 当点击未选中的图片时分两种情况
                if (AlbumActivity.selectedImgNum < 9) {
                    AlbumActivity.relay.accept(new EventType.SelectImageEvent(
                            path + imgs.get(holder.getAdapterPosition()),
                            true));
                } else {

                    holder.imageCb.setChecked(false);
                    Toast.makeText(context, "你最多只能选择9张图片", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void setCheckedImgs(List<String> checkedImgs) {
        this.checkedImgs = checkedImgs;
    }

    public void setPath(String path) {
        this.path = path;
    }

    @Override
    public int getItemCount() {
        return imgs.size();
    }

    public void clearData() {
        imgs.clear();
    }

    public void addData(List<String> addStrings) {
        imgs.addAll(addStrings);
    }

    class FolderImageHolder extends RecyclerView.ViewHolder {

        ImageView imageIv;
        AppCompatCheckBox imageCb;

        public FolderImageHolder(View itemView) {
            super(itemView);
            imageIv = (ImageView) itemView.findViewById(R.id.iv_img);
            imageCb = (AppCompatCheckBox) itemView.findViewById(R.id.rb_img);

            // 设置图片的高度
            ViewGroup.LayoutParams params = itemView.getLayoutParams();
            params.height = ScreenUtils.getScreenWidth(context) / 3;
            itemView.setLayoutParams(params);

        }
    }
}
